package net.wantongtech.xa.uploadbasicinfo.service;

import com.wtkj.vo.dzzf.base.xxdl.InteractionOrgMsgReceive;
import com.wtkj.zwxt.exceptions.BusinessException;
import lombok.extern.slf4j.Slf4j;
import net.wantongtech.xa.uploadbasicinfo.config.FastDFSConfig;
import net.wantongtech.xa.uploadbasicinfo.dao.DeleteHisFastDFSFileDao;
import org.csource.client.FastDFSClient;
import org.csource.common.MyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

import javax.annotation.Resource;
import java.util.List;

/**
 * 删除FastDFS历史文件服务实现
 *
 * @author MaShibo
 * 日期 :  2020/1/31
 */
@Slf4j
@Service
public class DeleteHisFastDFSFileService {


    /**
     * 每次查询的数量
     */
    private static final int QUERY_PER_NUM = 20000;

    @Resource
    private DeleteHisFastDFSFileDao deleteHisFastDFSFileDao;

    @Resource
    private FastDFSConfig fastDFSConfig;

    private final TransactionTemplate transactionTemplate;

    public DeleteHisFastDFSFileService(PlatformTransactionManager transactionManager) {
        this.transactionTemplate = new TransactionTemplate(transactionManager);
    }

    public void process() {
        while (true) {
            List<String> receiveIdList = deleteHisFastDFSFileDao.queryDeletingHisReceiveId(QUERY_PER_NUM);
            log.info("共查询到{}条待删除的接收表历史数据", receiveIdList.size());
            if (receiveIdList.isEmpty()) {
                return;
            }
            receiveIdList.forEach(receiveId -> {
                try {
                    transactionTemplate.execute(new TransactionCallbackWithoutResult() {
                        @Override
                        protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
                            try {
                                processSingle(receiveId);
                            } catch (MyException e) {
                                throw new BusinessException(e.getMessage());
                            }
                        }
                    });
                } catch (BusinessException e) {
                    log.error(e.getMessage());
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                }
            });
        }
    }

    private void processSingle(String receiveId) throws MyException {
        log.info("开始处理ListId为：{}的接收表数据", receiveId);
        InteractionOrgMsgReceive query = new InteractionOrgMsgReceive();
        query.setListID(receiveId);
        InteractionOrgMsgReceive receiveMsg = (InteractionOrgMsgReceive) deleteHisFastDFSFileDao.queryObject(query);
        if (receiveMsg == null) {
            log.info("listId为：{}的接收表数据不存在", receiveId);
            return;
        }

        //删除接收表数据
        deleteHisFastDFSFileDao.deleteObject(query);

        //删除对应FastDFS文件
        String storePath = receiveMsg.getStorePath();
        fastDFSConfig.init();
        FastDFSClient.deleteFile(storePath, null);
        log.info("ListId为：{}，对应FastDFS文件：{},删除成功", receiveId, storePath);
    }


}
